package com.example.taobaounion.presenter.impl;


import androidx.annotation.NonNull;

import com.example.taobaounion.model.Api;
import com.example.taobaounion.model.domain.SelectContent;
import com.example.taobaounion.model.domain.SelectedPageCategory;
import com.example.taobaounion.presenter.ISelectedPagePresenter;
import com.example.taobaounion.utils.RetrofitUtils;
import com.example.taobaounion.utils.UrlUtils;
import com.example.taobaounion.view.callback.ISelectedPageCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class SelectPagePresenter implements ISelectedPagePresenter {

    private ISelectedPageCallback mViewCallBack;
    private Api mApi;

    public SelectPagePresenter() {
        Retrofit retrofit = RetrofitUtils.getInstance().getRetrofit();

        mApi = retrofit.create(Api.class);
    }

    @Override
    public void getCategories() {

        Call<SelectedPageCategory> task = mApi.getSelectedPageCategories();

        task.enqueue(new Callback<SelectedPageCategory>() {
            @Override
            public void onResponse(@NonNull Call<SelectedPageCategory> call, @NonNull Response<SelectedPageCategory> response) {
                int code = response.code();

                if (code == HttpURLConnection.HTTP_OK) {
                    SelectedPageCategory category = response.body();

                    if (mViewCallBack != null) {
                        mViewCallBack.onCategoiesLoaded(category);
                    }
                } else {
                    if (mViewCallBack != null) {
                        mViewCallBack.onNetworkError();
                    }
                }
            }

            @Override
            public void onFailure(@NonNull Call<SelectedPageCategory> call, @NonNull Throwable t) {
                if (mViewCallBack != null) {
                    mViewCallBack.onNetworkError();
                }
            }
        });
    }

    @Override
    public void getContentByCategory(SelectedPageCategory.DataBean dataBean) {

        String selectedPageContentUrl = UrlUtils.getSelectedPageContentUrl(dataBean.getFavorites_id());

        Call<SelectContent> task = mApi.getSelectedPageContent(selectedPageContentUrl);

        task.enqueue(new Callback<SelectContent>() {
            @Override
            public void onResponse(@NonNull Call<SelectContent> call, @NonNull Response<SelectContent> response) {
                int code = response.code();

                if (code == HttpURLConnection.HTTP_OK) {
                    SelectContent content = response.body();

                    if (mViewCallBack != null) {
                        mViewCallBack.onContentLoaded(content);
                    }
                } else {
                    if (mViewCallBack != null) {
                        mViewCallBack.onNetworkError();
                    }
                }
            }

            @Override
            public void onFailure(@NonNull Call<SelectContent> call, @NonNull Throwable t) {
                if (mViewCallBack != null) {
                    mViewCallBack.onNetworkError();
                }
            }
        });
    }

    @Override
    public void reloadContent() {
        getCategories();
    }

    @Override
    public void registerViewCallback(ISelectedPageCallback callback) {
        this.mViewCallBack = callback;
    }

    @Override
    public void unRegisterViewCallback(ISelectedPageCallback callback) {
        mViewCallBack = null;
    }
}
